<?php
    /*
    * Copyright (c) 2007, Osuvaldo Ramos
    * 
    * All rights reserved.
    * 
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are
    * met:
    *
    *    * Redistributions of source code must retain the above copyright
    *      notice, this list of conditions and the following disclaimer.
    *    * Redistributions in binary form must reproduce the above copyright
    *      notice, this list of conditions and the following disclaimer in the
    *      documentation and/or other materials provided with the distribution.
    *    * Neither the name of the CoreTech nor the names of its
    *      contributors may be used to endorse or promote products derived from
    *      this software without specific prior written permission.
    * 
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * 
    * $Id: common.php 2 2008-01-21 02:48:28Z xdracox $
    */
    
    // we dont want anyone executing this script
    if ( !defined('BNETCS') || !defined('ROOT') ) exit();
    
    // current version
    define('VERSION', '0.0.1-alpha');
    
    // these are useful
    define('MINUTE', 60);
    define('HOUR', 60 * MINUTE);
    define('DAY', 24 * HOUR);
    define('WEEK', 7 * DAY);
    
    // setup PHP to report errors to our error handler
    error_reporting(E_ALL & ~E_NOTICE);
    
    $libPath = ROOT . '/lib';
    $smartyPath = $libPath . '/smarty';
    $pearPath = $libPath . '/pear';
    $ps = PATH_SEPARATOR;
    $newPath = get_include_path() . $ps . $libPath . $ps . $smartyPath . $ps
        . $pearPath;
    set_include_path($newPath);
    
    // include PEAR stuff
    require_once 'Auth.php';
    require_once 'MDB2.php';
    require_once 'PEAR.php';
    
    // Smarty
    require_once 'Smarty.class.php';
    
    // include my stuff :D
    require_once 'config.php';
    require_once 'ErrorHandler.php';
    require_once 'functions.php';
    
    // comment line below for debugging
    // set_error_handler(array('ErrorHandler', 'error'));
    
    // setup database connection
    $dsn = sprintf('%s://%s:%s@%s/%s', $CONF['db']['type'], $CONF['db']['user'],
        $CONF['db']['pass'], $CONF['db']['host'], $CONF['db']['name']);
    $mdb2 = &MDB2::singleton($dsn);
    
    if ( PEAR::isError($mdb2) )
    {
        $msg = $mdb2->getMessage();
        trigger_error($msg, E_USER_ERROR);
    }
    
    // db connected successfully, now we set the fetch mode
    $mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
    
    // salt the password (if any) before Auth is instantiated
    if ( isset($_POST['password']) )
    {
        // salt the password!
        if ( !get_magic_quotes_gpc() )
            $_POST['password'] = addslashes($_POST['password']);
        $_POST['password'] = salt_password($_POST['password'],
            $CONF['auth']['salt1'], $CONF['auth']['salt2']);
    }
    
    // now setup Auth
    $auth_options = array
    (
        'dsn' => $dsn,
        'table' => 'bnetcs_members',
        'usernamecol' => 'member_login',
        'passwordcol' => 'member_password',
        'db_fields' => '*',
        'cryptType' => 'md5'
    );
    
    $auth = &new Auth('MDB2', $auth_options);
    $auth->setShowLogin(false);
    $auth->start();
    
    // smarty also needs to be setup
    $smarty = new Smarty;
    $smarty->template_dir = $CONF['smarty']['template_dir']
        . '/' . $CONF['smarty']['template'];
    $smarty->compile_dir = $smarty->template_dir . '/' . 'compiled';
    
    // setup the title bar text and other info a template could use
    $title = $CONF['site']['titlebar'];
    $title = str_replace('%c', $CONF['clan']['name'], $title);
    
    $smarty->assign('page_title', $title);
    $smarty->assign('clan_name', $CONF['clan']['name']);
    $smarty->assign('version', VERSION);
    $smarty->assign('ROOT', $smarty->template_dir);
    
    if ( !file_exists($smarty->compile_dir) )
        mkdir($smarty->compile_dir);
    
    if ( isset($_SESSION['errors']) && sizeof($_SESSION['errors']) > 0 )
        $smarty->assign('errors', $_SESSION['errors']);
    
    if ( isset($_SESSION['notices']) && sizeof($_SESSION['notices']) > 0 )
        $smarty->assign('notices', $_SESSION['notices']);
    
    if ( $auth->checkAuth() )
        $smarty->assign('loggedin', true);
    else
        $smarty->assign('loggedin', false);
    
    unset($_SESSION['errors']);
    unset($_SESSION['notices']);
?>
